package org.yscz.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.yscz.common.HttpStatus;
import org.yscz.common.Result;
import org.yscz.entity.Project;
import org.yscz.entity.ProjectType;
import org.yscz.service.ProjectService;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目接口
 *
 * @author muqs
 * @create 2021-08-18 18:45
 */
@RestController
@Slf4j
@Api("项目接口")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 新增项目接口
     *
     * @param Project 前台传递的项目数据
     * @return Result对象
     */
    @PostMapping("/project/add")
    @ApiOperation(value = "新增项目接口", httpMethod = "POST")
    public Result add(@RequestBody Project Project) {
        log.info("Project:[{}]", Project);
        projectService.add(Project);
        return Result.success(HttpStatus.SUCCESS, "新增项目成功", "");
    }

    /**
     * 修改项目接口
     *
     * @param project 前台传递的项目数据
     * @return Result对象
     */
    @PostMapping("/project/modify")
    @ApiOperation(value = "修改项目接口", httpMethod = "POST")
    public Result modify(@RequestBody Project project) {
        log.info("project:[{}]", project);
        projectService.modify(project);
        return Result.success(HttpStatus.SUCCESS, "修改项目成功", "");
    }

    /**
     * 删除项目接口
     *
     * @param projectId 前台传递的项目数据
     * @return Result对象
     */
    @DeleteMapping("/project/del/{projectId}")
    @ApiOperation(value = "删除项目接口", httpMethod = "DELETE")
    public Result del(@PathVariable Long projectId) {
        log.info("projectId:[{}]", projectId);
        projectService.del(projectId);
        return Result.success(HttpStatus.SUCCESS, "删除项目成功", "");
    }

    /**
     * 查询项目列表接口
     *
     * @param projectName 项目名
     * @param pageNum     当前页
     * @param pageSize    每页数据条数
     * @return Result对象
     */
    @GetMapping("/project/list")
    @ApiOperation(value = "查询项目接口", httpMethod = "GET")
    public Result list(@RequestParam(value = "projectName", required = false) String projectName,
                       @RequestParam(value = "pageNum", required = false, defaultValue = "0") Integer pageNum,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        JSONObject reusltObj = new JSONObject();
        IPage<Project> page = new Page(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();

        if (!StringUtils.isEmpty(projectName)) {
            queryWrapper.like("project_name", projectName);
        }

        IPage<Project> ProjectPage = projectService.listPage(queryWrapper, page);

        reusltObj.put("list", ProjectPage == null ? new ArrayList<>() : ProjectPage.getRecords());
        reusltObj.put("total", ProjectPage == null ? 0 : ProjectPage.getTotal());

        return Result.success(HttpStatus.SUCCESS, "查询项目列表成功", reusltObj);
    }

    /**
     * 查询项目类别列表接口
     *
     * @return Result对象
     */
    @GetMapping("/project/projectTypeList")
    @ApiOperation(value = "查询项目类别接口", httpMethod = "GET")
    public Result projectTypeList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("project_type_id");
        List<ProjectType> projectTypes = projectService.projectTypeList(queryWrapper);
        return Result.success(HttpStatus.SUCCESS, "查询项目类别列表成功", projectTypes);
    }
}
